//
//  TTNoResultView.m
//  ExhibitionHall
//
//  Created by 王洋 on 2019/7/8.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "TTNoResultView.h"

@interface TTNoResultView ()

//UI控件
@property (nonatomic, strong) UIImageView *noResultImageView;
@property (nonatomic, strong) UILabel     *noResultDescLabel;
@property (nonatomic, strong) UIButton    *noResultActButton;

//外面传进来的变量
@property (nonatomic, copy) NSString *imageName;
@property (nonatomic, copy) NSString *desc;
@property (nonatomic, copy) NSString *btnTitle;

//传出去的点击事件
@property (nonatomic, copy) void (^didPressButton)(void);

@end

@implementation TTNoResultView

+ (instancetype)noResultInstance {
    TTNoResultView *noResultView = [[[self class] alloc] init];
    noResultView.backgroundColor = PP_Default_Color_Gray_F1F1F1;
    return noResultView;
}

- (void)showNoResultInView:(UIView *)view
                 imageName:(NSString *)imageName
                   message:(NSString *)message
                  btnTitle:(NSString *)btnTitle
            didPressButton:(void (^)(void))didPressButton {
    
    [view addSubview:self];
    [view bringSubviewToFront:self];
    [self addSubview:self.noResultImageView];
    [self addSubview:self.noResultDescLabel];
    [self addSubview:self.noResultActButton];

    CGRect frame;
    frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
    self.frame = frame;
    
    [self.noResultImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.centerY.mas_equalTo(-100);
    }];
    
    [self.noResultDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.noResultImageView.mas_bottom).offset(35);
        make.centerX.mas_equalTo(0);
    }];

    [self.noResultActButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.noResultDescLabel.mas_bottom).offset(30);
        make.centerX.mas_equalTo(0);        
        make.width.mas_equalTo([Utilities getStringWidthWithString:btnTitle fontSize:18 andHeight:22] + 30);
    }];
    
    [self.noResultImageView setImage:[UIImage imageNamed:imageName]];
    self.noResultDescLabel.text = message;
    
    if (btnTitle.length > 0) {
        [self.noResultActButton setTitle:btnTitle forState:UIControlStateNormal];
        self.didPressButton = didPressButton;

    } else {
        self.noResultActButton.hidden = YES;
    }
}

- (void)hideNoResult {
    [self removeFromSuperview];
}

- (void)btnOnClicked {
    if (self.didPressButton) {
        self.didPressButton();
    }
}

- (UIImageView *)noResultImageView {
    if (!_noResultImageView) {
        _noResultImageView = [[UIImageView alloc] init];
    }
    return _noResultImageView;
}

- (UILabel *)noResultDescLabel {
    if (!_noResultDescLabel) {
        _noResultDescLabel = [[UILabel alloc] init];
        _noResultDescLabel.textColor = UIColorHex(0x999999);
    }
    return _noResultDescLabel;
}

- (UIButton *)noResultActButton {
    if (!_noResultActButton) {
        _noResultActButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _noResultActButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _noResultActButton.layer.cornerRadius = 3;
        [_noResultActButton setTitleColor:UIColorHex(0x666666) forState:UIControlStateNormal];
        [_noResultActButton setBackgroundColor:UIColorHex(0xd2d0d0)];
        [_noResultActButton addTarget:self action:@selector(btnOnClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _noResultActButton;
}
@end
